package com.fzb.forum4j.dao.impl;

import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.fzb.forum4j.dao.api.ICategoryDao;
import com.fzb.forum4j.po.Category;
import com.fzb.forum4j.po.QueryMode;
@Repository
public class CategoryDaoImpl extends HibBaseImpl<Category> implements ICategoryDao {

	@Override
	public int getCategoryIdByCatelog(String catalog) {
		return (Integer) getSession().createQuery("select t.categroyId from Category t where t.catalog=:catalog").setParameter("catalog", catalog).uniqueResult();
	}
	
	/** 利用递归实现楼中楼
	 * @param rid
	 * @param data
	 * @return
	 */
	@SuppressWarnings("unchecked")
	private List<Map<String, Object>> getCategoryByCategoryId(Integer rid, List<Map<String, Object>> data) {
		data=getSession().createQuery("select new map(t.categoryName as categoryName,t.categroyId as categroyId,t.catalog as catalog) from Category t where t.parentCategroy.categroyId=:categroyId").setParameter("categroyId",rid).list();
		for (Map<String, Object> map : data) {
			List<Map<String,Object>> tlist=getCategoryByCategoryId((Integer) map.get("categroyId"),data);
			if(!tlist.isEmpty()){
				map.put("childCategory", tlist);
			}
		}
		return data;
	}
	
	@Override
	public void getCategoryTree(QueryMode<Category> qmd) {
		
		List<Map<String,Object>> data=getSession().createQuery("select new map(t.categoryName as categoryName,t.categroyId as categroyId,t.catalog as catalog) from Category t where t.parentCategroy=null").list();
		for (Map<String, Object> map : data) {
			List<Map<String,Object>> tlist=getCategoryByCategoryId((Integer) map.get("categroyId"),data);
			if(!tlist.isEmpty()){
				map.put("childCategory", tlist);
			}
		}
		qmd.getData().put("categorys", data);
	}
 

}
 